﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using Newtonsoft.Json;
using PSDLib.Models;
using PSDManager.SystemSettings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager
{
    public partial class FrmCheckDataIntegrity : Form
    {
        private Thread checkThread = null;
        public dl_task Task { get; set; }
        public List<dl_task_channel> Channels { get; set; }

        public List<dl_files> lackFiles { get; set; }
        public FrmCheckDataIntegrity()
        {
            InitializeComponent();
            lackFiles = new List<dl_files>();
            this.Shown += FrmCheckDataIntegrity_Shown;
        }

        private void FrmCheckDataIntegrity_Shown(object sender, EventArgs e)
        {
            checkThread = new Thread(new ThreadStart(CheckData));
            checkThread.Start();
        }

        private void CheckData()
        {
            var del = new EventHandler(delegate (object sender, EventArgs e)
            {

                foreach (var ch in Channels)
                {
                    Application.DoEvents();

                    lblInfo.Text = "正在检测【" + Task.task_name + " - " + ch.channel_name + "】";

                    DateTime now = DateTime.Now;

                    if (ch.end_time != null && (now - ch.end_time.Value).TotalSeconds > 0)
                    {
                        now = ch.end_time.Value;
                    }

                    int days = Convert.ToInt32((now - ch.start_time).TotalDays);

                    pbIntegrity.Minimum = 0;
                    pbIntegrity.Maximum = days;
                    pbIntegrity.Value = 0;

                    for (int i = 0; i < days; i++)
                    {
                        DateTime? st = null;
                        DateTime? et = null;

                        if (i == 0)
                        {
                            st = ch.start_time;
                            et = new DateTime(st.Value.Year, st.Value.Month, st.Value.Day, 0, 0, 0).AddDays(1);
                        }
                        else
                        {
                            st = et;
                            et = et.Value.AddDays(1);
                        }

                        var result = HttpHelper.Get<HResult<List<dl_files>>>(AppSettings.Setting.API.CheckDataIntegrity
                            + "?channelId=" + ch.id
                            + "&startTime=" + st.Value.ToString("yyyy-MM-dd HH:mm:ss")
                            + "&endTime=" + et.Value.ToString("yyyy-MM-dd HH:mm:ss"));

                        if (result.success && result.data != null)
                        {
                            foreach (var file in result.data)
                            {
                                var item = new ListViewItem(new string[] {
                                    (lvwIntegrity.Items.Count + 1).ToString(),
                                    file.channel_name,
                                    file.video_start_time.ToString("yyyy-MM-dd HH:mm:ss"),
                                    file.video_end_time.ToString("yyyy-MM-dd HH:mm:ss")
                                });

                                item.Tag = file;
                                item.Checked = true;

                                lvwIntegrity.Items.Add(item);
                            }

                            Application.DoEvents();
                        }
                        pbIntegrity.Value = i;
                    }
                }

                lblInfo.Text = "下载任务数据完整性校验";
            });

            if (this.InvokeRequired)
            {
                this.Invoke(del, null, null);
            }
            else
            {
                del(null, null);
            }

        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            lackFiles.Clear();

            foreach (ListViewItem item in this.lvwIntegrity.CheckedItems)
            {
                lackFiles.Add(item.Tag as dl_files);
            }

            if (lackFiles.Count > 0)
            {
                HttpHelper.Post<HResult>(AppSettings.Setting.API.CreateFiles, JsonConvert.SerializeObject(lackFiles));
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (btnCheck.Text == "停止")
            {
                btnCheck.Text = "开始";

                try
                {
                    checkThread.Abort();
                }
                catch { }
            }
            else
            {
                btnCheck.Text = "停止";

                checkThread = new Thread(new ThreadStart(CheckData));
                checkThread.Start();
            }
        }

    }
}
